<?php

class Bank extends M_Controller
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
        if ((int)$this->uid < 1) {
            apiSuccess(-4001, '请登录');
        }
    }

    // 添加银行卡
    public function addBank()
    {
        try {
            $result = $this->cardField();
            $result['uid'] = $this->uid;
            if (empty($this->models('bank')->addCard($result))) {
                throw new Exception('添加失败');
            }
            apiSuccess(1, '添加成功');
        } catch (Exception $e) {
            apiSuccess(0, $e->getMessage());
        }
    }

    // 修改银行卡
    public function editBank()
    {
        try {
            $info = $this->bank();
            $result = $this->cardField();
            if (empty($this->models('bank')->editCard($info['id'], $result))) {
                throw new Exception('操作失败');
            }
            apiSuccess(1, '修改成功');
        } catch (Exception $e) {
            apiSuccess(0, $e->getMessage());
        }
    }

    // 删除银行卡
    public function delBank()
    {
        try {
            $info = $this->bank();
            if (empty($this->models('bank')->delCard($info['id']))) {
                throw new Exception('操作失败');
            }
            apiSuccess(1, '成功');
        } catch (Exception $e) {
            apiSuccess(0, $e->getMessage());
        }
    }

    // 详情
    public function bankInfo()
    {
        try {
            $info = $this->bank();
            apiSuccess(1, 'success', $info);
        } catch (Exception $e) {
            apiSuccess(0, $e->getMessage());
        }
    }


    // 银行卡列表
    public function bankList()
    {
        $page = max($_GET['page'], 1);
        $pageSize = (int)$_GET['pageSize'] ?: 10;
        $result = $this->models('bank')->cardList($this->uid, $page, $pageSize);
        apiSuccess(1, 'success', [
            'list' => $result,
            'end' => $pageSize > count($result)
        ]);
    }

    /**
     * 银行卡详情
     * Bank constructor.
     * @throws Exception
     */
    protected function bank()
    {
        $id = $this->input->get('id');
        if (empty($id)) {
            throw new Exception('请选择银行卡');
        }
        $info = $this->models('bank')->cardInfo($id);
        if ($info['uid'] <> $this->uid) {
            throw new Exception('身份验证失败');
        }
        return $info;
    }

    /**
     * 银行卡字段
     * @return mixed
     * @throws Exception
     */
    protected function cardField()
    {
        $data = $this->input->post();
        $field = [
            ['name' => 'name', 'msg' => '户名', 'validate' => '/[\x{4e00}-\x{9fa5}]+/u'],
            ['name' => 'id_card', 'msg' => '银行卡号码', 'validate' => "/^[1-9]\d*$/"],
            ['name' => 'bank_name', 'msg' => '开户行名称', 'validate' => '/[\x{4e00}-\x{9fa5}]+/u'],
        ];
        foreach ($field as &$f) {
            if (empty($data[$f['name']])) {
                throw new Exception('请输入'.$f['msg']);
            } elseif ($f['validate'] && !preg_match($f['validate'], $data[$f['name']])) {
                throw new Exception($f['msg'] . '格式不正确');
            }
        }
        return $data;
    }
}